<?php
class backdoor extends CI_Controller{
    public function __constuctor(){
        parent::__construct();
    }
    public function index(){
        $this->load->view("backdoor_v");
    }
    public function auth(){
        $this->session->set_userdata(array("user"=>"admin"));
        $isOk=$this->session->userdata('user');
        if($isOk!=false){
                $this->load->model("event_md","event");
                $this->load->model("users_md","usersInst");
                $this->load->model("adminnotes_md","admin");
                $events=$this->event->getAllEvents();
                $users=$this->usersInst->getAllUsers();
                $adminNotes=$this->admin->getAllNotes();
                $data=array("events"=>$events,"users"=>$users,"adminNotes"=>$adminNotes);
                $this->load->view("admin_v",$data);
        }else{
             if(empty($_POST)){
                redirect(base_url()."index.php?/backdoor");
                return;
             }else{
                $cred=array("username"=>"admin","password"=>"v3v1v31saw3s0m3");
                $u=$_POST["username"];
                $p=$_POST["password"];
                if($cred["username"]==$u && $cred["password"]==$p){
                         $this->load->model("event_md","event");
                         $this->load->model("users_md","usersInst");
                         $this->load->model("adminnotes_md","admin");
                         $events=$this->event->getAllEvents();
                         $users=$this->usersInst->getAllUsers();
                         $adminNotes=$this->admin->getAllNotes();
                         $data=array("events"=>$events,"users"=>$users,"adminNotes"=>$adminNotes);
                $this->load->view("admin_v",$data);
                }else{
                    redirect(base_url()."index.php?/backdoor");
                }
             }
        }
    }
    public function addEvent(){
        $_POST["tStamp"]=strtotime($_POST["tStamp"]);
        $_POST["tStamp"]=date('Y-m-d H:i:s', $_POST["tStamp"]);
     
       $insertStr= $this->db->insert_string("vevents",$_POST);
       $result=$this->db->simple_query($insertStr);
       if($result){
            redirect(base_url()."index.php/backdoor/auth");
       }else{
           show_404();
           exit;
           //need to throw some error here
       }
    }
    public function deleteEvent(){
        $eids=explode(";",$_POST["eids"]);
        $eidsAr=array();
        foreach($eids as $id){
            array_push($eidsAr,substr($id, strpos($id,"-")+1));
        }
        $this->load->model("event_md","event");
        $result=$this->event->deleteEvent($eidsAr);
        echo $result;
    }
    
    public function filterUser(){
        $this->load->model("users_md","usersInst");
        $rs=$this->usersInst->getUserFilteredByEvent($_POST['eid']);
        $html=$this->generateUserMarkup($rs);
        echo $html;
    }
    private function generateUserMarkup($data){
        $html="";
        foreach($data as $single){
            $html.="<div id='peopel-".$single->userId."' class='people'>".
                    "<ul>".
                    "<li>Name:</li>".
                    "<li>".$single->fName." , ".$single->lName."</li>".
                    "</ul>".

                    "<ul>".
                    "<li>Location:</li>".
                    "<li>".$single->userCity." , ".$single->userState." ".$single->userZip."</li>".
                    "</ul>".

                    "<ul>".
                    "<li>Email:</li>".
                    "<li>".$single->email."</li>".
                    "</ul>".

                    "<ul>".
                    "<li>User Interest:</li>".
                    "<li>".$single->userInterest."</li>".
                    "</ul>".

                    "<ul>".
                    "<li>User Bio:</li>".
                    "<li>".$single->userBio."</li>".
                    "</ul>".
                    "</div>";
        }
        return $html;
    }
}
?>
